package com.wbao.dianniu.utils;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;


public class JsonUtil {
	private static GsonBuilder gsonBuilder = new GsonBuilder();
	private static Gson gson = getJson();
	private static Gson booleanGson = getBooleanJson();
	
	
	public static <T> T toObject(String json,Class<T> obj){
//		Gson gson = getJson();
		T t = null;
		try {
			t = gson.fromJson(json, obj);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return t;
	}
	
	public static <T> T toObject(Object jsonObj,Class<T> obj){
		T t = null;
		try {
			t =  gson.fromJson((JsonElement)jsonObj, obj);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return t;
	}

	public static <T> T toBooleanObject(Object jsonObj,Class<T> obj){
//		Gson gson = getJson();
		T t = null;
		try {
			t =  new Gson().fromJson(booleanGson.toJson(jsonObj), obj);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return t;
	}
	
	public static  <T> List<T> toObjectList(String json,Class<T> cls){
		List<T> list = new ArrayList<T>();
		try {
//			Gson gson = getJson();
			JsonArray arry = new JsonParser().parse(json).getAsJsonArray();
			for (JsonElement jsonElement : arry) {
				list.add(gson.fromJson(jsonElement, cls));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public  static  <T> List<T> toObjectList(Object jsonObj,Class<T> cls){
//		Gson gson = getJson();
		List<T> list = new ArrayList<T>();
		try {
			JsonArray arry = new JsonParser().parse(gson.toJson(jsonObj)).getAsJsonArray();
			for (JsonElement jsonElement : arry) {
				list.add(gson.fromJson(jsonElement, cls));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public static Gson  getJson(){
		gsonBuilder.registerTypeAdapter(Double.class,  new JsonSerializer<Double>() {
			
		    public JsonElement serialize(Double src, Type typeOfSrc,
		                JsonSerializationContext context) {
		            return new JsonPrimitive(Long.valueOf(src.longValue()).toString());
		        }
		  
		    });
		Gson gs = gsonBuilder.create();
		return gs;
	}
	
	public static Gson  getBooleanJson(){
		gsonBuilder.registerTypeAdapter(Boolean.class,  new JsonSerializer<Boolean>() {
			
		    public JsonElement serialize(Boolean src, Type typeOfSrc,
		                JsonSerializationContext context) {
		    	boolean sr = src.booleanValue();
		            return new JsonPrimitive(Boolean.valueOf(sr).toString());
		        }
		  
		    });
		Gson gs = gsonBuilder.create();
		return gs;
	}
	
}
